#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QSqlDatabase>
#include<QDebug>
#include<QSqlQuery>
#include<QSqlRecord>
#include<QSqlResult>
#include<QTcpSocket>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    ,server(new QTcpServer(this))
{
    ui->setupUi(this);

    server->setMaxPendingConnections(500);
    server->listen(QHostAddress::Any,8000);

    connect(server,&QTcpServer::newConnection,[this](){
       QTcpSocket *client = server->nextPendingConnection();
       connect(client,&QTcpSocket::connected,[](){});

       connect(client,&QTcpSocket::readyRead,[this,client](){

//           sender();//convert to QTcpSocket client 1
           QString str(client->readAll());
           QString info = ui->display->toPlainText();
           info.append("收到客户端请求:"+str+"\n");
           QSqlQuery q;
           if(q.exec(str)){
               info.append("执行请求成功\n");
                client->write("request success\n");
               if(q.isSelect()){
                   QString qStr; //= ui->queryLed->toPlainText();
                   while (q.next()) {
    //                    qStr.append(query.result()->handle().toString());//""
                       QSqlRecord s= q.record();
                       for(int i=0;i<s.count()-1;i++){
                           qStr.append(s.fieldName(i)+":"+s.value(i).toString()+",");
                       }
                       qStr.append(s.fieldName(s.count()-1)+":"
                                   +s.value(s.count()-1).toString()
                                   +";\n");

                   }
                   ui->queryLed->setPlainText(qStr);
                   client->write(qStr.toUtf8());
               }
           }else{
               client->write("request failed\n");
               info.append("执行请求失败\n");
           }
           ui->display->setPlainText(info);

       });
    });
    qDebug() << QSqlDatabase::drivers();
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");  //添加sqlite3驱动

//    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

//    db.setHostName("localhost");    // 主机地址
//        db.setPort(3306);              // 端口
//        db.setDatabaseName("test_db");  // 数据库名
//        db.setUserName("root");        // 用户名
//        db.setPassword("123456");      // 密码

    db.setDatabaseName("gg.db");

    bool ok = db.open();
    if(!ok){
         qDebug()<<"open gg.db failed";
    }


}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_exec_clicked()
{
    QString str = ui->execInput->toPlainText();
    if(str.isEmpty()){
        return;
    }


    QString info = ui->display->toPlainText();

    info.append("execute:"+str+"\n");

        QSqlQuery query;

        bool ok =  query.exec(str);

        if(ok){

            qDebug()<<"success:"<<str;
            info.append("success...\n");
//            qDebug() << query.size();
//            qDebug() << query.value(0);
//            qDebug() << query.next();
//            qDebug() << query.record();
//            qDebug() << query.result()->handle().toString();

            if(query.isSelect()){
                QString qStr; //= ui->queryLed->toPlainText();
                while (query.next()) {
//                    qStr.append(query.result()->handle().toString());//""
                    QSqlRecord s= query.record();
                    for(int i=0;i<s.count()-1;i++){
                        qStr.append(s.fieldName(i)+":"+s.value(i).toString()+",");
                    }
                    qStr.append(s.fieldName(s.count()-1)+":"
                                +s.value(s.count()-1).toString()
                                +";\n");

                }
                ui->queryLed->setPlainText(qStr);
            }

        }else{
            qDebug()<<"failed:"<<str;
            info.append("failed...\n");
        }

        ui->display->setPlainText(info);

}
